package cn.csust.net2.manager.server.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;

import cn.csust.net2.manager.server.dao.ResearchAnswerDAO;
import cn.csust.net2.manager.server.dao.ResearchDAO;
import cn.csust.net2.manager.server.dao.ResearchResultDAO;
import cn.csust.net2.manager.server.dao.ResearchTitleDAO;
import cn.csust.net2.manager.shared.exception.BeyondAuthorityException;
import cn.csust.net2.manager.shared.po.Research;
import cn.csust.net2.manager.shared.po.ResearchAnswer;
import cn.csust.net2.manager.shared.po.ResearchResult;
import cn.csust.net2.manager.shared.po.ResearchTitle;
import cn.csust.net2.manager.shared.service.ResearchCommonService;
import cn.csust.net2.manager.shared.util.Constant;
import cn.csust.net2.manager.shared.util.ServiceConstant;


/*
 * 此为research 和 researchManager 的公共服务类
 */
@Service(value = ServiceConstant.SERVICE_NAME_RESEARCHCOMMON)
public class ResearchCommanServiceImpl extends BaseServiceImpl implements ResearchCommonService{
	@Resource(name=Constant.DAO_NAME_RESEARCH)
	private ResearchDAO researchDAO;
	@Resource(name=Constant.DAO_NAME_RESEARCHTITLE)
	private ResearchTitleDAO researchTitleDAO;
	@Resource(name=Constant.DAO_NAME_RESEARCHRESULT)
	private ResearchResultDAO researchResultDAO;
	@Resource(name=Constant.DAO_NAME_RESEARCHANSWER)
	private ResearchAnswerDAO researchAnswerDAO;
	@Override
	@Transactional
	public PagingLoadResult<Research> findResearchsByPaging(PagingLoadConfig config) throws BeyondAuthorityException{
	
		return this.findAll(Research.class.getName(), config);
	}

	@Override
	@Transactional
	public PagingLoadResult<ResearchResult> findResearchResultsByPaging(PagingLoadConfig config)throws BeyondAuthorityException {
		
		String researchID = config.get(RESEARCH);
		StringBuffer sql = new StringBuffer(" select * from researchresult r where 1=1 ");
		if(researchID!=null&&!researchID.equals(""))
			sql.append(" and r.titleID in ( select titleID from researchtitle where  researchID = "+researchID+")");
		return researchResultDAO.findAllByPagingBySQL(config, sql.toString(), ResearchResult.class);
		
	}



	public ResearchDAO getResearchDAO() {
		return researchDAO;
	}


	public void setResearchDAO(ResearchDAO researchDAO) {
		this.researchDAO = researchDAO;
	}


	public ResearchTitleDAO getResearchTitleDAO() {
		return researchTitleDAO;
	}


	public void setResearchTitleDAO(ResearchTitleDAO researchTitleDAO) {
		this.researchTitleDAO = researchTitleDAO;
	}

	public ResearchResultDAO getResearchResultDAO() {
		return researchResultDAO;
	}

	public void setResearchResultDAO(ResearchResultDAO researchResultDAO) {
		this.researchResultDAO = researchResultDAO;
	}

	@Override
	@Transactional
	public PagingLoadResult<ResearchTitle> findResearchTitlesByPaging(PagingLoadConfig config)throws BeyondAuthorityException {
		Long researchID = config.get(RESEARCHID);
		if(researchID !=null&&!researchID.equals("")){
			
			return 	researchTitleDAO.findAllByPaging(config, " from ResearchTitle c where c.research.researchId = "+researchID);
		
		}else{
			
			return 	this.findAll(ResearchTitle.class.getName(), config);
		}
	}

	@Override
	@Transactional
	public PagingLoadResult<ResearchAnswer> findResearchAnswersByPaging(
			PagingLoadConfig config)throws BeyondAuthorityException {
		Long reserachTitleID = config.get(RESEARCH_TITLEID);
		if(reserachTitleID !=null&&!reserachTitleID.equals("")){
			
			return 	researchAnswerDAO.findAllByPaging(config, " from ResearchAnswer c where c.title.titleId = "+reserachTitleID);
		
		}else{
			
			return 	this.findAll(ResearchAnswer.class.getName(), config);
		}

	}

	public ResearchAnswerDAO getResearchAnswerDAO() {
		return researchAnswerDAO;
	}

	public void setResearchAnswerDAO(ResearchAnswerDAO researchAnswerDAO) {
		this.researchAnswerDAO = researchAnswerDAO;
	}

	@Override
	public void saveStudent_ResearchByPaging(PagingLoadConfig config)throws BeyondAuthorityException {
		Long researchID = config.get(RESEARCHID);
		List<Long>	numberList = config.get(STUDENT_NUMBERL_LIST);
		if(researchID !=null&&!researchID.equals("")&&numberList!=null){
			for(Long numberID:numberList){
				String sql = "INSERT INTO `manager`.`student_research`(`studentNumber`,`researchID`) VALUES ( "+numberID+","+researchID+");";
				researchAnswerDAO.getCurrentSession().createSQLQuery(sql);
			}
		
		}
		
	}

	@Override
	public PagingLoadResult<Long> findStudent_ResearchByPaging(
			PagingLoadConfig config) throws BeyondAuthorityException{
		Long researchID = config.get(RESEARCHID);
		String query = "select studentNumber from student_research where researchID = "+researchID;
		return 	researchAnswerDAO.findAllByPagingBySQL(config,query, Long.class);
	}

	/*
	 * (non-Javadoc)
	 * @see cn.csust.net2.manager.shared.service.ResearchCommonService#findResearchResultsByAnswerID(java.lang.Long)
	 * 
	 * 这个方法先不写
	 */

	@Override
	public int findResearchResultsByAnswerID(Long l) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * (non-Javadoc)
	 * @see cn.csust.net2.manager.shared.service.ResearchCommonService#hasResearchResultsByStudnetNumber(java.lang.Long)
	 * 这个方法先不写
	 */
	@Override
	public boolean hasResearchResultsByStudnetNumber(Long l) {
		// TODO Auto-generated method stub
		return false;
	}

	




}
